package org.csu.measure.Analyze;

import org.csu.measure.domain.*;
import org.csu.measure.domain.Class;
import org.dom4j.Attribute;
import org.dom4j.Element;


import java.util.ArrayList;
import java.util.List;

/**
 * 解析XML
 */
public class XMLParse {

    private static Class targetClass;          //存储目标类
    private static Association association;    //存储正在解析的关联
    private static Dependency dependency;    //存储正在解析的依赖
    private static AttributeOfClass attributes;    // 存储正在解析的属性
    private static Operation operation;    // 存储正在解析的方法
    private static Parameter parameter;    // 存储正在解析的方法参数

    /**
     * 获取元素name
     * @param element
     * @return
     */
    public static String getName(Element element){
        Attribute nameAttribute = element.attribute("a:Name");
        String nameValue = nameAttribute.getValue();
        return nameValue;
    }

    /**
     * 获取元素id
     * @param element
     * @return
     */
    public static String getID(Element element){
        Attribute idAttribute = element.attribute("Id");
        String idValue = idAttribute.getValue();
        return idValue;
    }

//    /**
//     * 获取值类型
//     * @param element
//     * @return
//     */
//    public static String getType(Element element){
//        Attribute typeAttribute = element.attribute("type");
//        String typeValue = typeAttribute.getValue();
//        return typeValue;
//    }


    /**
     * 通过Id获取类名
     * @param element
     * @return
     */
    public static String getClassName(Element element, List<Class> classList){
        String id = element.attributeValue("Ref");
        for (Class c:classList
        ) {
            if(id.equals(c.getId())){
                return c.getName();
            }
        }
        return null;
    }

    /**
     * 解析继承关系
     * @param elementList
     * @param classList
     * @param generalization
     */
    public static void analyzeGeneralization(List<Element> elementList, List<Class> classList, Generalization generalization){
        for(Element element : elementList){
            if (element.getName().equals("Object1")){
                Element element1 = element.element("Class");
                generalization.setParent(getClassName(element1, classList));
            }else if(element.getName().equals("Object2")){
                Element element1 = element.element("Class");
                generalization.setSubclass(getClassName(element1, classList));
            }
        }
    }


    /**
     * 解析关联关系
     * @param elementList
     * @param classList
     * @param association
     */
    public static void analyzeAssociation(List<Element> elementList, List<Class> classList, Association association){
        for(Element element : elementList){
            if (element.getName().equals("Object1")){
                Element element1 = element.element("Class");
                association.setObject(getClassName(element1, classList));
            }else if(element.getName().equals("Object2")){
                Element element1 = element.element("Class");
                association.setRelatedObject(getClassName(element1, classList));
            }
        }
    }


    /**
     * 解析依赖关系
     * @param elementList
     * @param classList
     * @param dependency
     */
    public static void analyzeDependency(List<Element> elementList, List<Class> classList, Dependency dependency){
        for(Element element : elementList){
            if (element.getName().equals("Object1")){
                Element element1 = element.element("Class");
                dependency.setDependent(getClassName(element1, classList));
            }else if(element.getName().equals("Object2")){
                Element element1 = element.element("Class");
                dependency.setOwn(getClassName(element1, classList));
            }
        }
    }

    /**
     * 解析XML
     * @param elementList
     * @param targetClass
     */
    public static void analyzeClass(List<Element> elementList, Class targetClass){
        for (Element element : elementList) {
            if(element.getName().equals("Name")){//获取类名
                targetClass.setName(element.getText());
            }
            else if(element.getName().equals("Attributes")){
                List<Element> attributeList = element.elements("Attribute");
                for (Element attribute : attributeList) {
                    AttributeOfClass attribute1 = new AttributeOfClass();//新建一个属性实体

                    //一个属性的所有子节点
                    List<Element> aList = attribute.elements();
                    //遍历子节点
                    for(Element a : aList){
                        if(a.getName().equals("Name")){
                            attribute1.setName(a.getText());
                        }
                        else if(a.getName().equals("Attribute.Visibility")){
                            String visibility = a.getText();
                            if(visibility == "-"){
                                attribute1.setVisibility("privacy");
                            }else if(visibility == "+"){
                                attribute1.setVisibility("public");
                            }else if(visibility == "#"){
                                attribute1.setVisibility("protected");
                            }else if(visibility == ""){
                                attribute1.setVisibility("default");
                            }
                        }
                    }
                    targetClass.addAttributes(attribute1);//把得到的属性实体加入到类
                }

            }
            else if(element.getName().equals("Operations")){//获取类方法
                //方法的节点list
                List<Element> operationList = element.elements("Operation");
                for (Element operation : operationList) {
                    Operation operation1 = new Operation();//新建一个方法实体

                    //一个方法的所有子节点
                    List<Element> oList = operation.elements();
                    //遍历子节点
                    for(Element o : oList){
                        if(o.getName().equals("Name")) {//获取方法名
                            operation1.setName(o.getText());
                        }else if(o.getName().equals("ReturnType")){//获取方法的返回类型
                            operation1.setVisibility(o.getText());
                        }else if(o.getName().equals("Parameters")){//获取方法的参数列表
                            //参数的节点list
                            List<Element> parameters = o.elements("Parameter");
                            List<Parameter> parametersList = new ArrayList<>();
                            for (Element parameter : parameters) {
                                //一个参数的所有子节点
                                List<Element> pList = parameter.elements();
                                //遍历子节点
                                for (Element p:pList){

                                    if(p.getName().equals("Name")){
                                        Parameter parameter1 = new Parameter();
                                        parameter1.setName(p.getText());
                                        parametersList.add(parameter1);
                                    }
                                }

                            }
                            operation1.setParameters(parametersList);
                        }
                    }

                    targetClass.addOperations(operation1);;
                }
            }
        }
    }
}
